{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6ebca60b",
   "metadata": {},
   "source": [
    "## Kubernetes Clusterwide SSL Demo"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba016c14",
   "metadata": {},
   "source": [
    "### Setup\n",
    "\n",
    "Create a Kind cluster by using an ansible playbook from the project `ansible` folder.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bc29bc5",
   "metadata": {},
   "outputs": [],
   "source": [
    "!cd ../ansible && ansible-playbook playbooks/kind-cluster.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b12607ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "!cd ../ansible && ansible-playbook playbooks/setup-ecosystem.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b4d93216",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Release \"seldon-core-v2-crds\" has been upgraded. Happy Helming!\r\n",
      "NAME: seldon-core-v2-crds\r\n",
      "LAST DEPLOYED: Thu Jun 22 09:11:26 2023\r\n",
      "NAMESPACE: seldon-mesh\r\n",
      "STATUS: deployed\r\n",
      "REVISION: 2\r\n",
      "TEST SUITE: None\r\n"
     ]
    }
   ],
   "source": [
    "!helm upgrade --install seldon-core-v2-crds  ../k8s/helm-charts/seldon-core-v2-crds -n seldon-mesh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bcf13333",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "namespace/ns1 created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create namespace ns1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "edba01cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "namespace/ns2 created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create namespace ns2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "422fce1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME: seldon-v2-certs\r\n",
      "LAST DEPLOYED: Thu Jun 22 10:00:32 2023\r\n",
      "NAMESPACE: ns1\r\n",
      "STATUS: deployed\r\n",
      "REVISION: 1\r\n",
      "TEST SUITE: None\r\n"
     ]
    }
   ],
   "source": [
    "!helm install seldon-v2-certs ../k8s/helm-charts/seldon-core-v2-certs/ -n ns1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "df456290",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME: seldon-v2-certs\r\n",
      "LAST DEPLOYED: Thu Jun 22 09:11:36 2023\r\n",
      "NAMESPACE: ns2\r\n",
      "STATUS: deployed\r\n",
      "REVISION: 1\r\n",
      "TEST SUITE: None\r\n"
     ]
    }
   ],
   "source": [
    "!helm install seldon-v2-certs ../k8s/helm-charts/seldon-core-v2-certs/ -n ns2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6a9cdb85",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "security:\r\n",
      "  controlplane:\r\n",
      "    protocol: SSL\r\n",
      "    ssl:\r\n",
      "      server:\r\n",
      "        secret: seldon-controlplane-server\r\n",
      "        clientValidationSecret: seldon-controlplane-client\r\n",
      "      client:\r\n",
      "        secret: seldon-controlplane-client\r\n",
      "        serverValidationSecret: seldon-controlplane-server\r\n",
      "  envoy:\r\n",
      "    protocol: SSL\r\n",
      "    ssl:\r\n",
      "      upstream:\r\n",
      "        server:\r\n",
      "          secret: seldon-upstream-server\r\n",
      "          clientValidationSecret: seldon-upstream-client\r\n",
      "        client:\r\n",
      "          secret: seldon-upstream-client\r\n",
      "          serverValidationSecret: seldon-upstream-server\r\n",
      "      downstream:\r\n",
      "        client:\r\n",
      "          serverValidationSecret: seldon-downstream-server\r\n",
      "        server: \r\n",
      "          secret: seldon-downstream-server\r\n"
     ]
    }
   ],
   "source": [
    "!cat ../k8s/samples/values-tls-dataplane-controlplane-example.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "71bf922e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME: seldon-v2\r\n",
      "LAST DEPLOYED: Thu Jun 22 09:11:40 2023\r\n",
      "NAMESPACE: seldon-mesh\r\n",
      "STATUS: deployed\r\n",
      "REVISION: 1\r\n",
      "TEST SUITE: None\r\n"
     ]
    }
   ],
   "source": [
    "!helm install seldon-v2 ../k8s/helm-charts/seldon-core-v2-setup/ -n seldon-mesh --set controller.clusterwide=true --values ../k8s/samples/values-tls-dataplane-controlplane-example.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "e28147b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME: seldon-v2-runtime\r\n",
      "LAST DEPLOYED: Thu Jun 22 10:00:36 2023\r\n",
      "NAMESPACE: ns1\r\n",
      "STATUS: deployed\r\n",
      "REVISION: 1\r\n",
      "TEST SUITE: None\r\n"
     ]
    }
   ],
   "source": [
    "!helm install seldon-v2-runtime ../k8s/helm-charts/seldon-core-v2-runtime  -n ns1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "2864216b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME: seldon-v2-servers\r\n",
      "LAST DEPLOYED: Thu Jun 22 10:00:36 2023\r\n",
      "NAMESPACE: ns1\r\n",
      "STATUS: deployed\r\n",
      "REVISION: 1\r\n",
      "TEST SUITE: None\r\n"
     ]
    }
   ],
   "source": [
    "!helm install seldon-v2-servers ../k8s/helm-charts/seldon-core-v2-servers  -n ns1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4423b5f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME: seldon-v2-runtime\r\n",
      "LAST DEPLOYED: Thu Jun 22 09:11:49 2023\r\n",
      "NAMESPACE: ns2\r\n",
      "STATUS: deployed\r\n",
      "REVISION: 1\r\n",
      "TEST SUITE: None\r\n"
     ]
    }
   ],
   "source": [
    "!helm install seldon-v2-runtime ../k8s/helm-charts/seldon-core-v2-runtime  -n ns2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "477c43a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME: seldon-v2-servers\r\n",
      "LAST DEPLOYED: Thu Jun 22 09:12:16 2023\r\n",
      "NAMESPACE: ns2\r\n",
      "STATUS: deployed\r\n",
      "REVISION: 1\r\n",
      "TEST SUITE: None\r\n"
     ]
    }
   ],
   "source": [
    "!helm install seldon-v2-servers ../k8s/helm-charts/seldon-core-v2-servers  -n ns2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5f781131",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "server.mlops.seldon.io/mlserver condition met\n",
      "server.mlops.seldon.io/triton condition met\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s server --all -n ns1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "af734add",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "server.mlops.seldon.io/mlserver condition met\n",
      "server.mlops.seldon.io/triton condition met\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s server --all -n ns2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d05ee2b",
   "metadata": {},
   "source": [
    "### Setup TLS Config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3835b3dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'172.21.255.2'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MESH_IP=!kubectl get svc seldon-mesh -n ns1 -o jsonpath='{.status.loadBalancer.ingress[0].ip}'\n",
    "MESH_IP_NS1=MESH_IP[0]\n",
    "import os\n",
    "os.environ['MESH_IP_NS1'] = MESH_IP_NS1\n",
    "MESH_IP_NS1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "33f7a263",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'172.21.255.4'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MESH_IP=!kubectl get svc seldon-mesh -n ns2 -o jsonpath='{.status.loadBalancer.ingress[0].ip}'\n",
    "MESH_IP_NS2=MESH_IP[0]\n",
    "import os\n",
    "os.environ['MESH_IP_NS2'] = MESH_IP_NS2\n",
    "MESH_IP_NS2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "181faf47",
   "metadata": {},
   "outputs": [],
   "source": [
    "!seldon config add tls ${PWD}/config-dataplane-tls.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "5db513b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "!seldon config activate tls"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2243a193",
   "metadata": {},
   "source": [
    "### Launch model in namespace ns1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fc5fdee5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/iris created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create -f ./models/sklearn-iris-gs.yaml -n ns1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5c1328c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/iris condition met\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s model --all -n ns1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "d048aa51",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "\t\"model_name\": \"iris_1\",\r\n",
      "\t\"model_version\": \"1\",\r\n",
      "\t\"id\": \"77f21360-b6cf-4c50-a000-1f219f5e9e29\",\r\n",
      "\t\"parameters\": {},\r\n",
      "\t\"outputs\": [\r\n",
      "\t\t{\r\n",
      "\t\t\t\"name\": \"predict\",\r\n",
      "\t\t\t\"shape\": [\r\n",
      "\t\t\t\t1,\r\n",
      "\t\t\t\t1\r\n",
      "\t\t\t],\r\n",
      "\t\t\t\"datatype\": \"INT64\",\r\n",
      "\t\t\t\"parameters\": {\r\n",
      "\t\t\t\t\"content_type\": \"np\"\r\n",
      "\t\t\t},\r\n",
      "\t\t\t\"data\": [\r\n",
      "\t\t\t\t2\r\n",
      "\t\t\t]\r\n",
      "\t\t}\r\n",
      "\t]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris --inference-host ${MESH_IP_NS1}:80 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53ea1e35",
   "metadata": {},
   "source": [
    "### Launch model in namespace ns2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5e2e72aa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/iris created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create -f ./models/sklearn-iris-gs.yaml -n ns2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "6cb7f562",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/iris condition met\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s model --all -n ns2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "4f116773",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "\t\"model_name\": \"iris_1\",\r\n",
      "\t\"model_version\": \"1\",\r\n",
      "\t\"id\": \"902b8483-0ede-4136-97be-dbf266f05a0c\",\r\n",
      "\t\"parameters\": {},\r\n",
      "\t\"outputs\": [\r\n",
      "\t\t{\r\n",
      "\t\t\t\"name\": \"predict\",\r\n",
      "\t\t\t\"shape\": [\r\n",
      "\t\t\t\t1,\r\n",
      "\t\t\t\t1\r\n",
      "\t\t\t],\r\n",
      "\t\t\t\"datatype\": \"INT64\",\r\n",
      "\t\t\t\"parameters\": {\r\n",
      "\t\t\t\t\"content_type\": \"np\"\r\n",
      "\t\t\t},\r\n",
      "\t\t\t\"data\": [\r\n",
      "\t\t\t\t2\r\n",
      "\t\t\t]\r\n",
      "\t\t}\r\n",
      "\t]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer iris --inference-host ${MESH_IP_NS2}:80 \\\n",
    "  '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}' "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2177c110",
   "metadata": {},
   "source": [
    "## TearDown"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "11d99aac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io \"iris\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete -f ./models/sklearn-iris-gs.yaml -n ns1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "d91cd84a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io \"iris\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete -f ./models/sklearn-iris-gs.yaml -n ns2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "3a84da13",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "release \"seldon-v2-servers\" uninstalled\n",
      "release \"seldon-v2-runtime\" uninstalled\n",
      "release \"seldon-v2-certs\" uninstalled\n"
     ]
    }
   ],
   "source": [
    "!helm delete seldon-v2-servers -n ns1\n",
    "!helm delete seldon-v2-runtime -n ns1\n",
    "!helm delete seldon-v2-certs -n ns1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "a5e6e07c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "release \"seldon-v2-runtime\" uninstalled\n",
      "Error: uninstall: Release not loaded: seldon-v2-runtime: release: not found\n",
      "release \"seldon-v2-certs\" uninstalled\n"
     ]
    }
   ],
   "source": [
    "!helm delete seldon-v2-runtime -n ns2\n",
    "!helm delete seldon-v2-runtime -n ns2\n",
    "!helm delete seldon-v2-certs -n ns2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "4b13fb4a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "release \"seldon-v2\" uninstalled\r\n"
     ]
    }
   ],
   "source": [
    "!helm delete seldon-v2  -n seldon-mesh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "89b13117",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "release \"seldon-core-v2-crds\" uninstalled\r\n"
     ]
    }
   ],
   "source": [
    "!helm delete seldon-core-v2-crds -n seldon-mesh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "f06f0dac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "namespace \"ns1\" deleted\n",
      "namespace \"ns2\" deleted\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete namespace ns1\n",
    "!kubectl delete namespace ns2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6529925f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
